package cn.thornbird.orgsync.controller;

import cn.thornbird.orgsync.annotation.CurrentUser;
import cn.thornbird.orgsync.entity.User;
import cn.thornbird.orgsync.exception.BadRequestException;
import cn.thornbird.orgsync.exception.ForbiddenException;
import cn.thornbird.orgsync.exception.NotFoundException;
import cn.thornbird.orgsync.model.PageList;
import cn.thornbird.orgsync.model.Result;
import cn.thornbird.orgsync.model.UserData;
import cn.thornbird.orgsync.service.UserService;
import cn.thornbird.orgsync.vo.record.SimpleUserRecord;
import cn.thornbird.orgsync.vo.record.UserRecord;
import cn.thornbird.orgsync.vo.request.ChangePasswordRequest;
import cn.thornbird.orgsync.vo.request.ResetPasswordRequest;
import cn.thornbird.orgsync.vo.request.SignUpRequest;
import cn.thornbird.orgsync.vo.request.UserQueryRequest;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/user")
@Api
@AllArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("find/{id}")
    public Result<User> find(@CurrentUser @ApiIgnore UserData userData, @PathVariable String id)
            throws NotFoundException, ForbiddenException {
        if (!userData.getId().equals(id)) {
            userData.checkAuthority(UserData.ADMIN);
        }
        return Result.success(userService.find(id));
    }

    @GetMapping("findUsers")
    public Result<List<User>> findUsers(@CurrentUser @ApiIgnore UserData userData,
                                        @RequestParam("ids") List<String> ids) {
        return Result.success(userService.findUsers(ids));
    }

    @GetMapping("query")
    public Result<PageList<User>> query(@CurrentUser @ApiIgnore UserData userData, UserQueryRequest userQueryRequest,
                                        Integer pageNumber, Integer pageSize) throws ForbiddenException {
        userData.checkAuthority(UserData.ADMIN);
        return Result.success(userService.query(userQueryRequest, pageNumber, pageSize));
    }

    @GetMapping("queryUsers")
    public Result<List<User>> queryUsers(@CurrentUser @ApiIgnore UserData userData, String keyword, Integer maxSize) {
        return Result.success(userService.query(keyword, maxSize));
    }

    @PostMapping("save")
    public Result<User> save(@CurrentUser @ApiIgnore UserData userData, @RequestBody @Valid UserRecord userRecord)
            throws BadRequestException, NotFoundException, ForbiddenException {
        userData.checkAuthority(UserData.ADMIN);
        return Result.success(userService.save(userRecord));
    }

    @DeleteMapping("delete/{id}")
    public Result<User> delete(@CurrentUser @ApiIgnore UserData userData, @PathVariable String id)
            throws BadRequestException, NotFoundException, ForbiddenException {
        userData.checkAuthority(UserData.ADMIN);
        if (userData.getId().equals(id)) {
            throw new BadRequestException("不能删除当前用户");
        }
        return Result.success(userService.delete(id));
    }

    @PostMapping("updateUserInfo")
    public Result<User> updateUserInfo(@CurrentUser @ApiIgnore UserData userData,
                                       @RequestBody @Valid SimpleUserRecord userRecord)
            throws BadRequestException, NotFoundException {
        return Result.success(userService.updateUserInfo(userData.getId(), userRecord));
    }

    @PostMapping("changePassword")
    public Result<Date> changePassword(@CurrentUser @ApiIgnore UserData userData,
                                       @RequestBody @Valid ChangePasswordRequest changePasswordRequest)
            throws BadRequestException, NotFoundException {
        return Result.success(userService.changePassword(userData.getId(), changePasswordRequest));
    }

    @PostMapping("signUp")
    public Result<String> signUp(@RequestBody @Valid SignUpRequest request)
            throws BadRequestException, NotFoundException {
        return Result.success(userService.signUp(request).getId());
    }

    @PostMapping("resetPassword")
    public Result<String> resetPassword(@RequestBody @Valid ResetPasswordRequest request)
            throws BadRequestException, NotFoundException {
        User user = userService.resetPassword(request);
        return Result.success(user.getSuperuser() ? "admin" : user.getId());
    }

}
